//
//  YunDanCenterCell.m
//  Rabbit
//
//  Created 阿帕 on 2019/12/16.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "YunDanCenterCell.h"
//#import "UIButton+QiEventInterval.h"

@interface YunDanCenterCell ()
@property (weak, nonatomic) IBOutlet UILabel *danHaoLabel;
@property (weak, nonatomic) IBOutlet UILabel *faHuoAddressLabel;
@property (weak, nonatomic) IBOutlet UILabel *shouHuoAddressLabel;
@property (weak, nonatomic) IBOutlet UILabel *danJiaLabel;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UILabel *orgLabel;
@property (weak, nonatomic) IBOutlet UIView *buttonBgView;
@property (weak, nonatomic) IBOutlet UIView *line1;
@property (weak, nonatomic) IBOutlet UIView *line2;
@property (weak, nonatomic) IBOutlet UIView *lvLine;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *orgBottomLineHeight;
@property (strong, nonatomic) IBOutlet UILabel *carnumberLabel;

@end

@implementation YunDanCenterCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _danHaoLabel.textColor = Dark_TextColor;
    _faHuoAddressLabel.textColor = Dark_TextColor;
    _faHuoAddressLabel.numberOfLines = 2;
    _shouHuoAddressLabel.textColor = Dark_TextColor;
    _shouHuoAddressLabel.numberOfLines = 2;
    _danJiaLabel.textColor = Orange_Color;
    _danJiaLabel.numberOfLines = 2;
    _infoLabel.textColor = Light_TextColor;
    _infoLabel.numberOfLines = 3;
    _orgLabel.textColor = Dark_TextColor;
    _carnumberLabel.textColor = Dark_TextColor;
//    _infoLabel.adjustsFontSizeToFitWidth = YES;
    _line1.backgroundColor = BackgroundColor;
    _line2.backgroundColor = BackgroundColor;
    _lvLine.backgroundColor = Theme_ColorValue;

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 45)];
    scrollView.tag = 1000;
    [_buttonBgView addSubview:scrollView];
}

- (void)setModel:(YunDanCenterModel *)model {
    
    _model = model;
    
    if ([model.shipmentName isNotBlank]) {
        _orgLabel.hidden = NO;
        _orgLabel.text = [@"发货企业:  " stringByAppendingString:model.ordCreatedByName];
        _orgBottomLineHeight.constant = 40;
    } else {
        _orgLabel.hidden = YES;
        _orgBottomLineHeight.constant = 15;
    }
    if ([model.vehicleLicense isNotBlank]) {
        _carnumberLabel.hidden = NO;
        _carnumberLabel.text = [@"车牌号:  " stringByAppendingString:model.vehicleLicense];
    }else{
        _carnumberLabel.hidden = YES;
    }
    _danHaoLabel.text = CJStringWithFormat(@"运输单号：%@", model.orderCode);
    _faHuoAddressLabel.text = CJStringWithFormat(@"%@%@", model.shipperAddress, model.shipperDetailAddress);
    _shouHuoAddressLabel.text = CJStringWithFormat(@"%@%@", model.consigneeAddress, model.consigneeDetailAddress);
    if((![DCSpeedy isBlankString:model.teamName] && [[UserDefaultUtil valueForKey:DRIVER_TYPE] integerValue] != 1) || [model.fareInfoDisplay isEqualToString:@"0"]){
        DebugLog(@"%@",[UserDefaultUtil valueForKey:DRIVER_TYPE]);
        DebugLog(@"%@",model.teamName);
        _danJiaLabel.hidden = YES;
    }else{
        _danJiaLabel.hidden = NO;
    }
    _danJiaLabel.text = [NSString stringWithFormat:@"%.2f%@",
                         model.realPrice,
                         [NSString nullToString:CJStringWithFormat(@"%@", model.isTrunkName)]];
    
    NSString *weight = @"";
    
    if ([DCSpeedy isPureInt:model.weight]) {
        weight = model.weight;
    } else {
        weight = CJStringWithFormat(@"%.3f", [model.weight floatValue]);
    }
    
    _infoLabel.text = [NSString stringWithFormat:@"%@ %@%@",
                       [NSString nullToString:CJStringWithFormat(@"%@", model.coalTypeName)],
                       weight,
                       [NSString nullToString:CJStringWithFormat(@"%@", model.coalUnitName)]];
    
    //底部操作按钮 不固定个数 需要动态展示
    
    //    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 45)];
    //    scrollView.tag = 1000;
    //    [_buttonBgView addSubview:scrollView];
    
    UIScrollView *scrollView = [self viewWithTag:1000];
    if (scrollView) {
        //解决按钮重用问题
        [scrollView removeAllSubviews];
    }
    
    NSMutableArray *tabArray = [[NSMutableArray alloc] init];
    NSMutableArray *iconArray = [[NSMutableArray alloc] init];
    
    NSString *status = CJStringWithFormat(@"%ld",(long)_model.status);
    
    NSInteger driverType = [[UserDefaultUtil valueForKey:DRIVER_TYPE] integerValue];
    NSDictionary *userInfo = [UserDefaultUtil valueForKey:Login_Branch];
    NSInteger joinTeam = [[userInfo valueForKey:@"joinTeam"] integerValue];
    
    float btnWidth = 0;
    float x = 10;
    if ([_assignmentStatus isEqual:@"waitCar"] ||
        ([_assignmentStatus isEqual:@"all"] && [DCSpeedy isBlankString:model.driverCode])) {
        
        if ((driverType == 0 && joinTeam == 1) || driverType == 2) {
            //独立司机加入车队或车队司机
            [tabArray addObject:@"接单"];
            [iconArray addObject:@"gcy_icon_ydzx_jiedan"];
        }
        
        if (Identity_CarCaptain) {
            //独立司机加入车队或车队司机
            [tabArray addObject:@"派单"];
            [iconArray addObject:@"gcy_icon_ydzx_paidan"];
            
            [tabArray addObject:@"取消"];
            [iconArray addObject:@"gcy_icon_ydzx_cancel"];
            
            [tabArray addObject:@"异常上报"];
            [iconArray addObject:@"gcy_icon_ydzx_yichang"];
            
            [tabArray addObject:@"异常处理"];
            [iconArray addObject:@"gcy_icon_yundan_yccl"];
            
            [tabArray addObject:@"投诉货主"];
            [iconArray addObject:@"gcy_icon_ydzx_tousu"];

            [tabArray addObject:@"投诉记录"];
            [iconArray addObject:@"gcy_icon_ydzx_tousujilu"];
        }
        
        for (int i=0; i<tabArray.count; i++) {
            NSString *titleStr = tabArray[i];
            btnWidth = titleStr.length * 20 + 30;
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = 100 + i;
            [button setBackgroundColor:[UIColor whiteColor]];
            [button setTitle:tabArray[i] forState:UIControlStateNormal];
            button.titleLabel.font = font(14);
            [button setTitleColor:Light_TextColor forState:UIControlStateNormal];
            [button setImage:image(iconArray[i]) forState:UIControlStateNormal];
            button.frame = CGRectMake(x, 0, btnWidth, 45);
            button.titleLabel.adjustsFontSizeToFitWidth = YES;
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [scrollView addSubview:button];
            [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:3];
            
            x += titleStr.length * 20 + 30;
            
            if (tabArray.count == 1) {
                button.frame = CGRectMake((kScreenWidth-btnWidth)/2, 0, btnWidth, 45);
            }
            
            
        }
        
    } else {
        
        if ([status isEqual:@"1"]) {
            //待运输  
            if (model.isCancel == 0) {
                //取消按钮显示
                [tabArray addObject:@"取消"];
                [iconArray addObject:@"gcy_icon_ydzx_cancel"];
            }
            if (model.isCancel != 1 && [DCSpeedy isBlankString:model.driverCode] == NO && Identity_Driver) {
                //装货按钮显示
                [tabArray addObject:@"装货"];
                [iconArray addObject:@"gcy_icon_ydzx_zhuanghuo"];
            }
            //0独立司机 1车队 2车队司机
            if ([CJStringWithFormat(@"%@",[UserDefaultUtil valueForKey:DRIVER_TYPE]) isEqual:@"1"] && [DCSpeedy isBlankString:model.driverCode] == YES && model.isCancel != 1) {
                //派单按钮显示
                [tabArray addObject:@"派单"];
                [iconArray addObject:@"gcy_icon_ydzx_paidan"];
            }
            if (model.isCancel == 1 && (model.cancelType == 1 || model.cancelType == 2)) {
                //取消确认按钮显示
                [tabArray addObject:@"取消确认"];
                [iconArray addObject:@"gcy_icon_ydzx_cancel"];
            }
            if ([CJStringWithFormat(@"%@",[UserDefaultUtil valueForKey:DRIVER_TYPE]) isEqual:@"1"] && [DCSpeedy isBlankString:model.driverCode] == NO && model.isCancel != 1) {
                //调单按钮显示
                [tabArray addObject:@"调单"];
                [iconArray addObject:@"gcy_icon_ydzx_tiaodan"];
            }
            
        } else if ([status isEqual:@"2"]) {
            //运输中
            if (model.isLoadingConfirm == 1 && Identity_Driver) {
                //卸货按钮显示
                [tabArray addObject:@"卸货"];
                [iconArray addObject:@"gcy_icon_ydzx_xiehuo"];
            }
            //0独立司机 1车队 2车队司机
            if ([CJStringWithFormat(@"%@",[UserDefaultUtil valueForKey:DRIVER_TYPE]) isEqual:@"1"] && [DCSpeedy isBlankString:model.driverCode]) {
                //派单按钮显示
                [tabArray addObject:@"派单"];
                [iconArray addObject:@"gcy_icon_ydzx_paidan"];
            }
            if ([CJStringWithFormat(@"%@",[UserDefaultUtil valueForKey:DRIVER_TYPE]) isEqual:@"1"] && [DCSpeedy isBlankString:model.driverCode] == NO) {
                //调单按钮显示
                [tabArray addObject:@"调单"];
                [iconArray addObject:@"gcy_icon_ydzx_tiaodan"];
            }
            
        } else if ([status isEqual:@"3"] || [status isEqual:@"4"]) {
            //待结算 或者 待打款 司机端（isDriverComment） 货主端（isShipmentComment）
            if (model.isDriverComment == 0) {
                //评价按钮显示
                [tabArray addObject:@"评价"];
                [iconArray addObject:@"gcy_icon_ydzx_pingjia"];
            }
            if (model.isDriverComment == 1 || model.isShipmentComment == 1) {
                //查看评论按钮显示
                [tabArray addObject:@"查看评价"];
                [iconArray addObject:@"gcy_icon_ydzx_chakanpingjia"];
            }
            
            [tabArray addObject:@"催款"];
            [iconArray addObject:@"gcy_icon_ydzx_cuikuan"];
            
        } else if ([status isEqual:@"5"]) {
            //已完成 司机端（isDriverComment） 货主端（isShipmentComment）
            if (model.isDriverComment == 0) {
                //评价按钮显示
                [tabArray addObject:@"评价"];
                [iconArray addObject:@"gcy_icon_ydzx_pingjia"];
            }
            if (model.isDriverComment == 1 || model.isShipmentComment == 1) {
                //查看评论按钮显示
                [tabArray addObject:@"查看评价"];
                [iconArray addObject:@"gcy_icon_ydzx_chakanpingjia"];
            }
        }
        
        if (![status isEqual:@"5"]) {
            
            [tabArray addObject:@"投诉货主"];
            [tabArray addObject:@"投诉记录"];
            
            [iconArray addObject:@"gcy_icon_ydzx_tousu"];
            [iconArray addObject:@"gcy_icon_ydzx_tousujilu"];
            
        } else {
            [tabArray addObject:@"投诉记录"];
            [iconArray addObject:@"gcy_icon_ydzx_tousujilu"];
        }
        
        //运单状态：1已接单/2已装货/3已卸货/4已结算/5已打款', 4状态之前可以上报异常
        if ([status isEqual:@"1"] || [status isEqual:@"2"] || [status isEqual:@"3"]) {
            [tabArray addObject:@"异常上报"];
            [iconArray addObject:@"gcy_icon_ydzx_yichang"];
            
            [tabArray addObject:@"异常处理"];
            [iconArray addObject:@"gcy_icon_yundan_yccl"];
        }
        
//        [tabArray addObject:@"超10单处理"];
//        [iconArray addObject:@"gcy_icon_ydzx_tousujilu"];
        
        for (int i=0; i<tabArray.count; i++) {
            NSString *titleStr = tabArray[i];
            btnWidth = titleStr.length * 20 + 30;
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = 100 + i;
            [button setBackgroundColor:[UIColor whiteColor]];
            [button setTitle:tabArray[i] forState:UIControlStateNormal];
            button.titleLabel.font = font(14);
            [button setTitleColor:Light_TextColor forState:UIControlStateNormal];
            [button setImage:image(iconArray[i]) forState:UIControlStateNormal];
            button.frame = CGRectMake(x, 0, btnWidth, 45);
            button.titleLabel.adjustsFontSizeToFitWidth = YES;
            if ([button.titleLabel.text isEqualToString:@"装货"] || [button.titleLabel.text isEqualToString:@"卸货"]) {
                [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
                button.titleLabel.font = boldFont(18);
            }
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [scrollView addSubview:button];
            [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:3];
            
            x += titleStr.length * 20 + 30;
        }
    }
    
    //重新设置 scrollView 的容量
    scrollView.contentSize = CGSizeMake(x, 0);
    
}

- (void)setOrderStatus:(NSString *)orderStatus {
    _orderStatus = orderStatus;
    if ([orderStatus isEqual:@"6"]) {
        _buttonBgView.hidden = YES;
        _line1.hidden = YES;
    } else {
        _buttonBgView.hidden = NO;
        _line1.hidden = NO;
    }
}

- (void)buttonClick:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(buttonIndexClick:withButton:)]) {
        [self .delegate buttonIndexClick:button.tag withButton:button];
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
